package com.jt.web.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.ItemDesc;
import com.jt.util.ObjectMapperUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-12-11 15:41
 */
@RestController
@RequestMapping("/")
public class JsonPController {

    // 实现jsonP跨域访问
    // @RequestMapping("web/testJSONP")
    public String jsonp(String callback) {
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(101L).setItemDesc("跨域访问测试");
        String json = ObjectMapperUtil.toJson(itemDesc);
        return callback + "(" + json + ")";
    }

    @RequestMapping("web/testJSONP")
    public JSONPObject jsonp2(String callback) {
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(101L).setItemDesc("跨域访问测试");
        return new JSONPObject(callback, itemDesc);
    }

}
